package com.hitsoyauce.mail.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.mail.MessagingException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.hitsoyauce.mail.common.MailHelper;
import com.hitsoyauce.mail.common.TxtFileFilter;


public class MainWindow extends JFrame implements ActionListener {
	private static final long serialVersionUID = 101285272564330044L;
	
	private String filePath;
	
	private JButton btnSendMail;
	private JButton btnFileChoose;
	private JFileChooser chooserDialog;
	private JLabel lblPathName;
	private JLabel lblPath;
	private JLabel lblPrev;
	private JTextArea txtArea;
	private JScrollPane scrollPane;
	private JMenuBar menuBar;
	
	public MainWindow() {
		getContentPane().setLayout(null); //设置布局管理
		chooserDialog = new JFileChooser();
		scrollPane = new JScrollPane();
		
		btnSendMail = new JButton("发送日报");
		btnSendMail.setFont(new Font("宋体", Font.PLAIN, 12));
		btnSendMail.addActionListener(this);
		btnFileChoose = new JButton("内容选择");
		btnFileChoose.setFont(new Font("宋体", Font.PLAIN, 12));
		btnFileChoose.addActionListener(this);
		
		lblPathName = new JLabel("未选择文件");
		lblPathName.setFont(new Font("宋体", Font.PLAIN, 12));
		
		lblPath = new JLabel("文件路径：");
		lblPath.setFont(new Font("宋体", Font.PLAIN, 12));
		lblPrev = new JLabel("预览：");
		lblPrev.setFont(new Font("宋体", Font.PLAIN, 12));
		
		
		menuBar = new MenuBar(this);
		
		btnSendMail.setBounds(148, 350, 85, 30);
		btnFileChoose.setBounds(273, 350, 85, 30);
		lblPathName.setBounds(105, 21, 300, 15);
		lblPath.setBounds(35, 21, 60, 15);
		lblPrev.setBounds(35, 46, 54, 15);
		scrollPane.setBounds(35, 71, 437, 249);
		
		getContentPane().add(btnSendMail);
		getContentPane().add(btnFileChoose);
		getContentPane().add(lblPathName);
		getContentPane().add(lblPath);
		getContentPane().add(lblPrev);
		getContentPane().add(scrollPane);
		
		txtArea = new JTextArea();
		scrollPane.setViewportView(txtArea);
		txtArea.setFont(new Font("宋体", Font.PLAIN, 13));
		txtArea.setEditable(false);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			} 
		});
		
		setSize(515, 480);
		setLocation();
		
		setJMenuBar(menuBar);
		setTitle("日报助手");
		setResizable(false);
		setVisible(true);
		
		setDefaultPrev();
	}
	
	private void setDefaultPrev() {
		String defaultPath = MailHelper.getDefaultPath();
		
		if ("".equals(defaultPath)) {
			JOptionPane.showMessageDialog(null, "请设置文件路径！");
			return;
		}
		
		MailHelper.setFilePath(defaultPath);
        try {
			txtArea.setText(MailHelper.getMailText());
			lblPathName.setText(defaultPath);
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "请检查文件路径！");
		}
	}

	private void setLocation() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension size = getSize();
		int x = (screenSize.width - size.width) / 2;
		int y = (screenSize.height - size.height) / 2;
		setLocation( x, y );
	}
	
	
	public void showFileChooser() {
		chooserDialog.setFileSelectionMode(0); //设定只能选择到文件  
		chooserDialog.setAcceptAllFileFilterUsed(false);
		chooserDialog.setFileFilter(new TxtFileFilter()); //只显示txt文件
		
        int state = chooserDialog.showOpenDialog(null); //此句是打开文件选择器界面的触发语句  
        if (state == JFileChooser.CANCEL_OPTION) return;
        
        filePath = chooserDialog.getSelectedFile().getAbsolutePath();//f为选择到的文件  
        
        MailHelper.setFilePath(filePath);
        lblPathName.setText(filePath);
        try {
			txtArea.setText(MailHelper.getMailText());
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "请检查文件路径！");
		}
	}
	
	private void send() {
		try {
			if (MailHelper.getFilePath() == null) {
				JOptionPane.showMessageDialog(null, "请检查文件路径！");
				return;
			}
			
			MailHelper.sendMessage();
		} catch (IOException io) {
			io.printStackTrace();
			JOptionPane.showMessageDialog(null, "请检查文件路径！");
			return;
		} catch (MessagingException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "日报发送失败，请检查账户设置是否正确！");
			return;
		}
		
		JOptionPane.showMessageDialog(null, "日报已发送！");
	}
	
	public void refreshPrev() {
		try {
			if (txtArea.getText().equals("")) return;
			
			txtArea.setText(MailHelper.getMailText());
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "请检查文件路径！");
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//发送
		if (e.getSource().equals(btnSendMail)) {
			send();
			return;
		}
		
		//日报选择
		if (e.getSource().equals(btnFileChoose)) {
			showFileChooser();
		}
	}

}